
base = C

BNFC     = bnfc
PDFLATEX = pdflatex
HAPPY    = happy -gca
ALEX     = alex -g
GHC      = ghc


bnfcGenerHS   = Abs$(base).hs Print$(base).hs Test$(base).hs ErrM.hs
bnfcGenerated = $(bnfcGenerHS) Doc$(base).tex Par$(base).y Lex$(base).x
generatedHS   = Par$(base).hs Lex$(base).hs $(bnfcGenerHS)


.PHONY : all
all: $(generatedHS) pdf

.PHONY : pdf
pdf: Doc$(base).pdf

.PHONY : test
test: Test$(base)
	-./Test$(base) Test.rs

.PHONY : clean
clean:
	-rm -fv *.hi *.o *.log *.aux *.bak

.PHONY : cleanhaskell
cleanhaskell:
	-rm -fv $(generatedHS)
	-rm -fv *.{happy,alex}log

.PHONY : distclean
distclean: clean cleanhaskell
	-rm -fv $(bnfcGenerated)
	-rm -fv {Layout,XML,Doc}$(base).*
	-rm -fv $(base).dtd Test$(base)
	-rm -fv ErrM.* SharedString.*

.PRECIOUS : Par%.y Lex%.x Doc%.tex

Doc%.tex Par%.y Lex%.x Abs%.hs Print%.hs Test%.hs : %.cf
	$(BNFC) $<
# Fix errors of the generated LaTeX file
	sed "s|\\^|\\^{}|g" Doc$*.tex > temp$<.temp
	@mv -v temp$<.temp Doc$*.tex
	-@rm -fv *.bak

%.hs: %.y
	$(HAPPY) $< -i$*.happylog

%.hs: %.x
	$(ALEX) $< -i$*.alexlog

%.pdf: %.tex
	$(PDFLATEX) $<
	-rm -fv $*.{log,aux}

Test% : Par%.hs Lex%.hs Abs%.hs Print%.hs Test%.hs ErrM.hs
	$(GHC) --make $@.hs -o $@
